package com.ruoyi.system.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.Gift;
import com.ruoyi.system.domain.ProfitHistory;
import com.ruoyi.system.domain.UserInfo;
import com.ruoyi.system.service.IGiftService;
import com.ruoyi.system.service.IProfitHistoryService;
import com.ruoyi.system.service.IUserInfoService;
import com.ruoyi.system.tool.operationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 礼物模块Controller
 * 
 * @author kinglu
 * @date 2021-11-30
 */
@RestController
@RequestMapping("/GiftModular")
public class GiftModularController extends BaseController
{
    @Autowired
    private IGiftService giftService;

    @Autowired
    private IProfitHistoryService profitHistoryService;

    @Autowired
    private IUserInfoService userInfoService;

    /**
     * 赠送礼物
     */
    @PostMapping("/givegift")
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public AjaxResult givegift(@RequestBody ProfitHistory profitHistory)
    {
        //判空
        if(profitHistory.getGetUserId()==null){
            return AjaxResult.error("主播编号不能为空");
        }else if(profitHistory.getSetUserId()==null){
            return AjaxResult.error("用户编号不能为空");
        }else if(profitHistory.getGiftId()==null){
            return AjaxResult.error("礼物编号不能为空");
        }else if(profitHistory.getGiftNum()==null||profitHistory.getGiftNum()==0){
            return AjaxResult.error("礼物数量不能为空");
        }
        //查找礼物和用户
        Gift gift = giftService.selectGiftByGiftId(profitHistory.getGiftId());
        UserInfo userInfo = userInfoService.selectUserInfoByUserId(profitHistory.getSetUserId());
        Long price = profitHistory.getGiftNum() * gift.getGiftPrice();
        //判断用户余额
        if(price>userInfo.getCoin()){
            return AjaxResult.error("余额不足");
        }
        //新增赠送历史记录
        profitHistory.setMemo(price.toString());
        profitHistory.setGiftTime(new Date());
        profitHistoryService.insertProfitHistory(profitHistory);
        //减少用户余额
        userInfo.setCoin(userInfo.getCoin()-price);
        userInfoService.updateUserInfo(userInfo);
        //增加主播余额
        userInfo = userInfoService.selectUserInfoByUserId(profitHistory.getGetUserId());
        userInfo.setCoin(userInfo.getCoin()+price);
        userInfoService.updateUserInfo(userInfo);
        return AjaxResult.success("赠送成功");
    }

    /**
     * 查询用户收益记录
     */
    @GetMapping("/getProfitHistory")
    public TableDataInfo getProfitHistory(Long userId)
    {
        if(userId==null){
            throw  new operationException("用户编号为空");
        }
        ProfitHistory profitHistory = new ProfitHistory();
        profitHistory.setGetUserId(userId);
        profitHistory.setSetUserId(userId);
        startPage();
        List<ProfitHistory> list = profitHistoryService.selectProfitHistoryListUser(profitHistory);
        return getDataTable(list);
    }

    /**
     * 查询礼物列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Gift gift)
    {
        startPage();
        List<Gift> list = giftService.selectGiftList(gift);
        return getDataTable(list);
    }

}
